<?php
/**
 * Created by PhpStorm.
 * User: m1514
 * Date: 2017/1/1
 * Time: 16:54
 */

namespace Admin\Controller;


use Common\Model\CargoListModel;
use Common\Model\GenrdAttrModel;

class GoodsListController extends LoginController
{
    public function index()
    {

        $gid=I('get.gid');
        //展示页
        $pps=(new CargoListModel())->where(['gid'=>$gid])->select();
//        dd($pps);
        foreach ($pps as $k=>$v){
            $pps[$k]['attr']=M("goods_attr")->where(['gtid'=>array( 'in',$v['combine'])])->order('gtid desc')->getField('gtvalue',true);
        }
        $this->assign('pps',$pps);
//        dd($pps);

#===============================================================================================================================================
        //关联类型属性表和商品属性表
        $data=(new GenrdAttrModel())->alias('ta')->join("shop_goods_attr ga on ta.taid=ga.taid")->where("ga.gid={$gid} and ta.class=1")->select();
        $temp=[];
        //循环出颜色和尺码压入$temp中
        foreach($data as $k=>$v){
            $temp[$v['taname']][]=$v;
        }
        //分配
        $this->assign('temp',$temp);
//        dd($temp);
#================================================================================================================================================
        //添加动作
        if(IS_POST){
            $data=I('post.');
            $gid=I('get.gid');
            $data['gid']=$gid;
            foreach($data['combine'] as $k=>$v){
                $data['combine']=$v['0'].','.$v['1'];
            }
            $this->store(new CargoListModel(),$data);
        }
        //显示模板
        $this->display();
    }
    //编辑
    public function edit(){
        //接收get中的glid
        $glid=I('get.glid');
        //获取旧数据
        $pps=(new CargoListModel())->where(['glid'=>$glid])->select();
        //循环$pps查找出与combine字段中对应的商品属性表中的gtid字段然后排序，如果不排序尺寸和颜色会颠倒位置
        foreach ($pps as $k=>$v){
            $pps[$k]['attr']=M("goods_attr")->where(['gtid'=>array( 'in',$v['combine'])])->order('gtid desc')->getField('gtvalue',true);
        }
        $pps['0']['ax']=explode(',',$pps['0']['combine']);
        //分配到页面
//        dd($pps);die;
        $this->assign('pps',$pps);
        //获取pps中的gid为下面的查询语句提供查询参数
        $gid=$pps['0']['gid'];
        $datalist=(new GenrdAttrModel())->alias('ta')->join("shop_goods_attr ga on ta.taid=ga.taid")->where("ga.gid={$gid} and ta.class=1")->select();
        $temp=[];
        //循环出颜色和尺码压入$temp中
        foreach($datalist as $k=>$v){
            $temp[$v['taname']][]=$v;
        }

        //分配
        $this->assign('temp',$temp);
        if(IS_POST){
            $data=I('post.');
            $data['glid']=$glid;
            foreach($data['combine'] as $k=>$v){
                $data['combine']=$v['0'] . ',' .$v['1'];
            }
            $this->store(new CargoListModel(),$data,function(){
                $this->success('编辑成功',u('index/',['gid'=>I('post.gid')]));die;
            });
        }

        //显示模板
        $this->display();
    }
    public function del(){
        $glid=I('get.glid');
        $data=(new CargoListModel())->delete($glid);
        if($data){
            $this->success('删除成功');
        }
    }
}